<template>
  <div class="view">
    <image src="~@/static/login/topBg.png" class="topBg" mode="widthFix" />
    <div class="content">
      <div class="top">
        <image class="welcome" src="~@/static/login/welcome.png" />
        <div class="text">数·智阅读生态体系服务系统</div>
      </div>

      <div class="card">
        <div class="item">
          <div class="label">账号</div>
          <u-input class="input" v-model="formData.account" placeholder="请输入账号"></u-input>
        </div>

        <div class="item">
          <div class="label">密码</div>
          <div class="inputBox">
            <u-input @suffix="changeShow" v-model="formData.password" :password="showPassword" class="input" placeholder="请输入密码"></u-input>
            <image class="eye" v-if="showPassword" @click="changeShow" src="../../static/login/close_eye.png" />
            <image class="eye" v-else @click="changeShow" src="../../static/login/open_eye.png" />
          </div>
        </div>

        <u-button class="login" type="primary" @click="open">登录</u-button>
      </div>
    </div>

    <SliderCaptcha v-model="visible" :options="options" :loading="loading" @success="success" @check="check" @close="close" @refresh="getSliderOptions" @error="getSliderOptions">
      <!-- vue3 -->
      <template #title>请完成安全验证</template>
      <template #successText>验证成功，登录中</template>
      <template #errorText><span style="color:rgb(232,78,78)">验证失败，请重试</span></template>
      <template #tips>拖动下方滑块完成拼图</template>
      <!-- <template #question>自定义提示</template> -->
      <!-- vue3 -->
    </SliderCaptcha>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import openEye from '../../static/login/open_eye.png'
import closeEye from '../../static/login/close_eye.png'
import { request } from '@/util/request'
import SliderCaptcha from '@/components/kkokk-slider-captcha/kkokk-slider-captcha.vue'
import { navigate } from '@/util/navigate'

const visible = ref(false)
const loading = ref(false)
const options = ref({})
const formData = reactive({
  key_str: "",
  account: '',
  password: ''
})
const responseData = ref()

function open() {
  visible.value = true
  getSliderOptions()
}

async function getSliderOptions() {
  loading.value = true
  try {
    let res = await request({
      url: 'slideVerify/getCaptcha',
      method: 'get',
      loading: false,
      data: {}
    })
    responseData.value = res.content
    formData.key_str = res.content.key
    let response = res.content
    options.value = {
      sliderImg: response.img,
      sliderKey: response.key,
      sliderY: response.y
    }
    loading.value = false
    return response
  } catch (err) {
  }

}

async function check(sliderKey, sliderX, done, error) {
  // 这里是验证是否成功的接口
  loading.value = true
  formData.captcha = sliderX
  try {
    let res = await request({
      url: 'adminMobile/login/login',
      method: 'post',
      loading: false,
      data: { ...formData }
    })
    if (res.code == 200) {
      loading.value = false
      uni.setStorageSync('userToken_zh_web', {
        ...res.content
      })

      setTimeout(() => {
        navigate({ url: '/pages/home/index' })
      }, 1000);
      done()
    }
  } catch (e) {
    error()
  }
}

function init() {
  getSlider()
}

const showPassword = ref(true)
function changeShow() {
  showPassword.value = !showPassword.value
}





</script>

<style lang="scss" scoped>
.topBg {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  width: 100vw;
  z-index: 0;
  height: 696rpx;
}

.content {
  .top {
    height: 240rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 24rpx;
    .text {
      font-size: 28rpx;
      font-family: PingFang SC, PingFang SC-Medium;
      font-weight: 500;
      text-align: center;
      color: #468eff;
      line-height: 44rpx;
    }
    .welcome {
      width: 164rpx;
      height: 40rpx;
    }
  }
  .card {
    box-sizing: border-box;
    height: 660rpx;
    background: #ffffff;
    border-radius: 20rpx;
    box-shadow: 0px 6rpx 12rpx 0rpx rgba(153, 153, 153, 0.4);
    padding: 0 30rpx;
    margin: 30rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    .item {
      .label {
        font-size: 28rpx;
        font-family: PingFang SC, PingFang SC-Bold;
        font-weight: 700;
        text-align: left;
        color: #333333;
        line-height: 44rpx;
        margin-bottom: 20rpx;
      }
      .input {
        border-radius: 16rpx;
        height: 80rpx;
      }
      .inputBox {
        display: flex;
        position: relative;
        .eye {
          position: absolute;
          right: 24rpx;
          top: 50%;
          transform: translateY(-50%);
          width: 32rpx;
          height: 32rpx;
        }
      }
    }
    .login {
      border-radius: 16rpx;
    }
  }
}
</style>